package com.lemon.smartsafetynew.ui.fragment.training;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.ldzs.recyclerlibrary.Mode;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.common.AppUtils;
import com.lemon.smartsafetynew.entity.training.OnlineVideoVo;
import com.lemon.smartsafetynew.ui.activity.video.PLVideoTextureActivity;
import com.lemon.smartsafetynew.ui.adapter.training.OnlineTrainingVideoAdapter;
import com.pili.pldroid.player.AVOptions;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2018/3/15.
 */

public class OnlineTrainingListFragment extends BasePullToRefreshFragment<OnlineVideoVo, List<OnlineVideoVo>>
        implements RecyclerTClickListener<OnlineVideoVo> {

    private static final String[] videoNameArray = new String[] {
            "动火作业安全培训",
            "受限空间作业安全培训",
            "吊装作业安全培训",
            "盲板抽堵作业安全培训",
            "高处作业安全培训",
            "断路作业安全培训",
            "动土作业安全培训",
            "临时用电作业安全培训"
    };

    private static final String[] videoUrlArray = new String[] {
            "http://58.215.171.233:18080/_large_huge_files/secure_video/1.mp4",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/2.mp4",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/3.mp4",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/4.mp4",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/5.mp4",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/6.mp4",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/7.mp4",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/8.mp4",
    };

    private static final String[] videoImageArray = new String[] {
            "http://58.215.171.233:18080/_large_huge_files/secure_video/video_1.JPG",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/video_2.JPG",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/video_3.JPG",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/video_4.JPG",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/video_5.JPG",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/video_6.JPG",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/video_7.JPG",
            "http://58.215.171.233:18080/_large_huge_files/secure_video/video_8.JPG"
    };

    public static OnlineTrainingListFragment newInstance() {

        Bundle args = new Bundle();

        OnlineTrainingListFragment fragment = new OnlineTrainingListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initViews() {
        super.initViews();

        mPullToRefreshRecyclerView.setRefreshMode(Mode.DISABLED);
        mDataList.clear();
        for(int i = 0; i < videoNameArray.length; i++) {
            OnlineVideoVo videoVo = new OnlineVideoVo(videoUrlArray[i], videoImageArray[i], videoNameArray[i]);
            mDataList.add(videoVo);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onItemClick(OnlineVideoVo data, int position) {
        if (AppUtils.isNetworkAvailable(getmContext())) {
            if (AppUtils.isWifi(getmContext())) {
                startToPlay(data);
            } else {
                showChooseDialog(getmContext().getResources().getString(R.string.wifi_tips), data);
            }
        } else {
            mHUDUtil.showErrorTip("网络异常，请检查网络。", new HUDUtil.CallBack() {
                @Override
                public void onCall() {

                }
            });
        }
    }

    /**
     * 跳转播放页面
     * @param data
     */
    private void startToPlay(OnlineVideoVo data) {
        Intent intent = new Intent(getmContext(), PLVideoTextureActivity.class);
        intent.putExtra("videoPath", data.getVideoUrl());
        intent.putExtra("mediaCodec", AVOptions.MEDIA_CODEC_SW_DECODE);
        intent.putExtra("liveStreaming", 0);
        startActivity(intent);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return false;
    }

    @Override
    public LemonRcvAdapter<OnlineVideoVo> getLemonAdapter() {
        return new OnlineTrainingVideoAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return null;
    }

    /**
     * dialog
     */
    private MaterialDialog chooseDialog;

    private void showChooseDialog(String remark, final OnlineVideoVo data) {
        chooseDialog = new MaterialDialog.Builder(getmContext())
                .title("提示")
                .content(remark)
                .negativeText("继续")
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        startToPlay(data);
                    }
                })
                .positiveText("取消")
                .show();
    }

    private void cancelDialog() {

        if (chooseDialog != null && chooseDialog.isShowing()) {
            chooseDialog.dismiss();
            chooseDialog = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        cancelDialog();
    }
}
